package pl.edu.agh.iosr.gamblingzone.mainpanel.action;

import javax.ejb.Stateless;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.component.UIData;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.log.Log;
import org.richfaces.component.UITree;
import org.richfaces.component.UITreeNode;
import org.richfaces.component.html.HtmlTree;
import org.richfaces.component.html.HtmlTreeNode;
import org.richfaces.event.AjaxSelectedEvent;
import org.richfaces.event.NodeSelectedEvent;
import org.richfaces.event.NodeSelectedListener;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import pl.edu.agh.iosr.gamblingzone.dao.DisciplineDAO;
import pl.edu.agh.iosr.gamblingzone.dao.GroupDAO;
import pl.edu.agh.iosr.gamblingzone.dao.ejb.DisciplineDAOBean;
import pl.edu.agh.iosr.gamblingzone.model.Discipline;
import pl.edu.agh.iosr.gamblingzone.model.Group;
import pl.edu.agh.iosr.gamblingzone.model.AbstractEvent;
import pl.edu.agh.iosr.gamblingzone.model.Participant;
import pl.edu.agh.iosr.gamblingzone.utils.DAOFactory;
import pl.edu.agh.iosr.gamblingzone.utils.EJBDAOFactoryBean;

@Stateless
@Name("treebean")
//@Scope(ScopeType.SESSION)

public class GroupsTreeAction implements GroupsTree, NodeSelectedListener {
	@PersistenceContext
	protected EntityManager entityManager;
	
	@In(required=true)
	EventsView eventsView;
	
	@In
	DAOFactory dao;
	
	private List<EventRowAction> events = new ArrayList<EventRowAction>();
	
	//@Out
	private TreeNode tree = null;
	
	@Logger
	Log log;
	

	private DisciplineDAO disciplineDAO;
	private List<Discipline> allDisciplines;

	private void setupDAO() {
        if (disciplineDAO == null) {
                disciplineDAO = new DisciplineDAOBean();
                ((DisciplineDAOBean) disciplineDAO).setEntityManager(entityManager);
        }
	}

	private List<Discipline> getAllDisciplines() {
        setupDAO();
        allDisciplines = disciplineDAO.findAll();
        return allDisciplines;
	}

	//@Create
	private void createTree() {
		tree = new GroupsTreeNodeImpl();
		List<Discipline> discs = getAllDisciplines();
		int counter=1;
		for(Iterator<Discipline> iterDisc = discs.iterator(); iterDisc.hasNext(); )
		{
			Discipline disc = iterDisc.next();
			GroupsTreeNodeImpl tmpNode = new GroupsTreeNodeImpl();
			TempData tmpData = new TempData();
			tmpData.setName(disc.getName());
			tmpData.setEntity(disc);
			tmpNode.setData(tmpData);
			tmpNode.setType(disc.getName());
			int grpCounter = 1;

			for(Iterator<Group>iterGrp = disc.getGroups().iterator(); iterGrp.hasNext(); )
			{
				Group grp = iterGrp.next();
				tmpData = new TempData();
				tmpData.setName(grp.getName());
				tmpData.setEntity(grp);
				GroupsTreeNodeImpl tmpGrpNode = new GroupsTreeNodeImpl();
				tmpGrpNode.setData(tmpData);
				tmpGrpNode.setType(grp.getName());
				
				tmpNode.addChild(grpCounter++, tmpGrpNode);
			}
			tree.addChild(counter++, tmpNode);
		}
		log.info("Created tree with "+counter+" nodes");
	}
	
	@Out
	public TreeNode getTree() {
		if (tree == null) {
			createTree();	
		}
		
		return tree;
	}

	public void setTree(TreeNode tree) {
		this.tree = tree;
	}
	
	public void processSelection(NodeSelectedEvent event) {
		System.out.println("===============================================");
		System.out.println("===============================================");
		System.out.println("===============================================");
		System.out.println("===============================================");

		if (!(event.getComponent() instanceof UIData)) {
			return;
		}
		
		UIData uidData = (UIData)event.getComponent();
		
		if (uidData.getRowData() == null || !(uidData.getRowData() instanceof TempData)) {
			return;
		}
		
		TempData tmData = (TempData)uidData.getRowData(); 

		if (!(tmData.getEntity() instanceof Group)) {
			return;
		}
		
		Group currGroup = (Group)tmData.getEntity();
		System.out.println("===================== O.K	 ==========================");
		
		if (dao == null) {
			dao = new EJBDAOFactoryBean();

		}
		GroupDAO groupDAO = dao.getGroupDAO();
		currGroup = groupDAO.makePersistent(currGroup);
		eventsView.setEvents(currGroup.getEvents());
	}
	
	@Out
	public List<EventRowAction> getEvents() {
		log.info("getEvents called, events count:"+this.events.size());
		return this.events;
	}
	
	public void setEvents(List<EventRowAction> events) {
		this.events = events;
	}
}
